package easy;

public class Solution_1021 {

    public String removeOuterParentheses(String s) {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0, begin = -1, count = 0; i < s.length(); i++) {
            if ('(' == s.charAt(i)) {
                count++;
            } else {
                count--;
            }
            if (count == 0) {
                if (i - begin > 2) {
                    buffer.append(s, begin + 2, i);
                }
                begin = i;
            }
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        Solution_1021 model = new Solution_1021();
        System.out.println(model.removeOuterParentheses("(()())(())"));
        System.out.println(model.removeOuterParentheses("(()())(())(()(()))"));
        System.out.println(model.removeOuterParentheses("()()"));
        System.out.println(model.removeOuterParentheses(""));
    }
}
